package com.yanqu.road.server.logic.recharge.limit;

import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public abstract class BaseRMBMallLimit {
    protected Logger logger = LogManager.getLogger(BaseRMBMallLimit.class.getName());
    protected long userId;
    protected MallInfo mallInfo;

    public BaseRMBMallLimit(long userId, MallInfo mallInfo) {
        this.userId = userId;
        this.mallInfo = mallInfo;
    }

    /**
     * 系统解锁
     * @return
     */
    protected abstract boolean isSystemUnLock();

    /**
     * 购买数量限制
     * @return
     */
    protected abstract LimitBuyResult buyCountLimit();

    /**
     * 特殊限制判断
     * @return
     */
    protected abstract int logicCheck();

    /**
     * 预处理下
     * @return
     */
    protected int pretreatment(){
        if(null == mallInfo){
            return GameErrorCode.E_MALL_NO_EXIST;
        }
        return 0;
    }

    public LimitBuyResult checkMallLimit(){
        LimitBuyResult limitBuyResult = new LimitBuyResult();
        //预处理数据
        int resultCode = pretreatment();
        if(0 != resultCode){
            limitBuyResult.setRet(resultCode);
            return limitBuyResult;
        }
        //系统未解锁
        if(!isSystemUnLock()){
            limitBuyResult.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return limitBuyResult;
        }
        resultCode = logicCheck();
        if(0 != resultCode){
            limitBuyResult.setRet(resultCode);
            return limitBuyResult;
        }
        //购买数量限制
        limitBuyResult = buyCountLimit();
        return limitBuyResult;
    }

    /**
    * 描述：活动商品限购
    * 作者：zrq
    * 时间：2023/2/11
    * 参数：

    * 返回值：
    **/
    protected LimitBuyResult activityMallCountLimit(GamePlayer player) {
        LimitBuyResult limitBuyResult = new LimitBuyResult();
        if(-1 == mallInfo.getLimitBuyCount()){
            limitBuyResult.setLimitCount(Integer.MAX_VALUE);
            return limitBuyResult;
        }
        if(null == player){
            limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
            return limitBuyResult;
        }
        ActivityMallModule activityMallModule = player.getModule(ActivityMallModule.class);
        if (activityMallModule.isOverLimitBuyCount(mallInfo, 1)) {//限购
            limitBuyResult.setRet(GameErrorCode.E_MALL_LIMIT_BUY);
            return limitBuyResult;
        }
        limitBuyResult.setLimitCount(activityMallModule.getLimitCount(mallInfo));
        return limitBuyResult;
    }
}
